package com.example.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.commonutils.JwtUtils;
import com.example.educenter.entity.UcenterMember;
import com.example.educenter.entity.vo.RegisterVo;
import com.example.educenter.mapper.UcenterMemberMapper;
import com.example.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.educenter.utils.MD5;
import com.example.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author wlg
 * @since 2021-09-08
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //登录
    @Override
    public String login(UcenterMember member) {

        //获取登录手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        //手机和密码非空判断
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"登录失败");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if(mobileMember==null){
            throw new GuliException(20001,"登录失败，没有次手机号");

        }
        //判断密码
        //数据库密码采用md5加密
        if(!mobileMember.getPassword().equals(MD5.encrypt(password))){
            throw  new GuliException(20001,"登录失败，密码错误");
        }
        //判断用户是否禁用
        if(mobileMember.getIsDisabled()){
            throw new GuliException(20001,"登录失败,账号被禁用");
        }

        //登录成功
        //生成token字符串，使用jwt工具
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());


        return jwtToken;
    }

    //注册
    @Override
    public void register(RegisterVo registerVo) {
          //获取注册数据
        String code=registerVo.getCode();//验证码
        String mobile=registerVo.getMobile();//手机号
        String nickname=registerVo.getNickname();//昵称
        String password=registerVo.getPassword();//密码

       if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)
          ||StringUtils.isEmpty(code)||StringUtils.isEmpty(nickname)){
           throw new GuliException(20001,"注册失败");
       }
       //判断验证码
        //获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
       if(!code.equals(redisCode)){
           throw new GuliException(20001,"注册失败,验证码错误");
       }
    //判断手机号是否重复，表里面存在相同的手机号，则不能进行添加
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
       wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count>0){
            throw new GuliException(20001,"注册失败,手机号在数据库中已经存在");
        }

        //数据添加数据库中
        UcenterMember member=new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        baseMapper.insert(member);
    }

    //根据openid判断
    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    //查询某一天注册人数，给service_statistics模块调用
    @Override
    public Integer countRegister(String day) {

        return baseMapper.countRegister(day);
    }
}
